﻿namespace WMS6.Web.Analytics
{
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.AnalyticsReportServiceRef;
    using WMS6.Web.UserControls;

    public class CustomerReturnDetailRptList : BasePage
    {
        protected Button btnExport;
        protected Button btnList;
        protected Button btnRefresh;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F15371;
        protected GridView gvReturn;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected Label lblCustomerReturnDetailRptList;
        protected Label lblMsg;
        protected Label lblTotalPrice;
        protected Label lblTotalPriceText;
        protected Label lblTotalReturn;
        protected Label lblTotalReturnText;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        private decimal TotalPrice;
        private decimal TotalReturn;
        protected UpdatePanel up1;

        private void BindData2gvReturnList(CustomerReturnDetailRptQueryEntity customerReturnDetailRptQueryEntity)
        {
            this.PagerFooter.CurrentPage = int.Parse(customerReturnDetailRptQueryEntity.CurrentPage);
            DataSet ds = AnalyticsReportServiceClientProxy.GetInstance().GetCustomerReturnDetailRptDataSet(customerReturnDetailRptQueryEntity);
            base.BindData2GridView(this.gvReturn, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            CustomerReturnDetailRptQueryEntity customerReturnDetailRptQueryEntity = this.GetCustomerReturnDetailRptQueryEntity();
            customerReturnDetailRptQueryEntity.IsGetAll = true;
            this.hdnActionID.Value = "EXPORT";
            string currentPageIndex = customerReturnDetailRptQueryEntity.CurrentPage;
            customerReturnDetailRptQueryEntity.CurrentPage = "0";
            this.gvReturn.Columns[1].Visible = false;
            this.gvReturn.AllowSorting = false;
            this.BindData2gvReturnList(customerReturnDetailRptQueryEntity);
            this.gvReturn.ShowFooter = false;
            customerReturnDetailRptQueryEntity.IsGetAll = false;
            customerReturnDetailRptQueryEntity.CurrentPage = currentPageIndex;
            this.SetCustomerReturnDetailRptQueryEntity(customerReturnDetailRptQueryEntity);
            base.Export2Excel("CustomerReturnDetailRpt.xls", this.gvReturn);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            CustomerReturnDetailRptQueryEntity customerReturnDetailRptQueryEntity = this.GetCustomerReturnDetailRptQueryEntity();
            customerReturnDetailRptQueryEntity.CurrentPage = "0";
            this.BindData2gvReturnList(customerReturnDetailRptQueryEntity);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            CustomerReturnDetailRptQueryEntity customerReturnDetailRptQueryEntity = this.GetCustomerReturnDetailRptQueryEntity();
            customerReturnDetailRptQueryEntity.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2gvReturnList(customerReturnDetailRptQueryEntity);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            CustomerReturnDetailRptQueryEntity customerReturnDetailRptQueryEntity = this.GetCustomerReturnDetailRptQueryEntity();
            customerReturnDetailRptQueryEntity.OWNER_ID = "";
            customerReturnDetailRptQueryEntity.SKU_CLASS = "";
            customerReturnDetailRptQueryEntity.SKU_ID = "";
            customerReturnDetailRptQueryEntity.SKU_DESCR = "";
            customerReturnDetailRptQueryEntity.CUSTOMER_ID = "";
            customerReturnDetailRptQueryEntity.CUSTOMER_NAME = "";
            customerReturnDetailRptQueryEntity.StartDateTime = "";
            customerReturnDetailRptQueryEntity.EndDateTime = "";
            this.BindData2gvReturnList(customerReturnDetailRptQueryEntity);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            CustomerReturnDetailRptQueryEntity customerReturnDetailRptQueryEntity = this.GetCustomerReturnDetailRptQueryEntity();
            if (int.Parse(customerReturnDetailRptQueryEntity.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                customerReturnDetailRptQueryEntity.CurrentPage = (int.Parse(customerReturnDetailRptQueryEntity.CurrentPage) + 1).ToString();
                this.BindData2gvReturnList(customerReturnDetailRptQueryEntity);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            CustomerReturnDetailRptQueryEntity customerReturnDetailRptQueryEntity = this.GetCustomerReturnDetailRptQueryEntity();
            if (int.Parse(customerReturnDetailRptQueryEntity.CurrentPage) > 0)
            {
                customerReturnDetailRptQueryEntity.CurrentPage = (int.Parse(customerReturnDetailRptQueryEntity.CurrentPage) - 1).ToString();
                this.BindData2gvReturnList(customerReturnDetailRptQueryEntity);
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            CustomerReturnDetailRptQueryEntity customerReturnDetailRptQueryEntity = this.GetCustomerReturnDetailRptQueryEntity();
            this.BindData2gvReturnList(customerReturnDetailRptQueryEntity);
        }

        private CustomerReturnDetailRptQueryEntity GetCustomerReturnDetailRptQueryEntity()
        {
            return (base.PageQueryEntitySession as CustomerReturnDetailRptQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                CustomerReturnDetailRptQueryEntity customerReturnDetailRptQueryEntity = this.GetCustomerReturnDetailRptQueryEntity();
                customerReturnDetailRptQueryEntity.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2gvReturnList(customerReturnDetailRptQueryEntity);
            }
        }

        protected void gvReturn_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F15371", this.gvReturn);
        }

        protected void gvReturn_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                e.Row.Attributes.Add("ondblclick", "JavaScript:document.getElementById('" + ((CheckBox) e.Row.FindControl("chbSelect")).ClientID + "').click();");
                if (this.hdnActionID.Value != "EXPORT")
                {
                    base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblSkuDescr"));
                }
                if (((Label) e.Row.FindControl("lblEeceivedQty")) != null)
                {
                    string qty = ((Label) e.Row.FindControl("lblEeceivedQty")).Text;
                    this.TotalReturn += decimal.Parse(string.IsNullOrEmpty(qty) ? "0" : qty);
                }
                if (((Label) e.Row.FindControl("lblDiffQty")) != null)
                {
                    string price = ((Label) e.Row.FindControl("lblDiffQty")).Text;
                    this.TotalPrice += decimal.Parse(string.IsNullOrEmpty(price) ? "0" : price);
                }
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                this.lblTotalReturnText.Text = base.GetPreferenceDigit(this.TotalReturn);
                this.lblTotalPriceText.Text = base.GetPreferenceDigit(this.TotalPrice);
            }
        }

        protected void gvReturn_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                CustomerReturnDetailRptQueryEntity customerReturnDetailRptQueryEntity = this.GetCustomerReturnDetailRptQueryEntity();
                customerReturnDetailRptQueryEntity.SortField = e.SortExpression;
                if (customerReturnDetailRptQueryEntity.SortDirection == "ASC")
                {
                    customerReturnDetailRptQueryEntity.SortDirection = "DESC";
                }
                else if (customerReturnDetailRptQueryEntity.SortDirection == "DESC")
                {
                    customerReturnDetailRptQueryEntity.SortDirection = "ASC";
                }
                this.BindData2gvReturnList(customerReturnDetailRptQueryEntity);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            CustomerReturnDetailRptQueryEntity customerReturnDetailRptQueryEntity = new CustomerReturnDetailRptQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    customerReturnDetailRptQueryEntity.OWNER_ID = this.PreviousPage.OwnerID;
                    customerReturnDetailRptQueryEntity.SKU_CLASS = this.PreviousPage.SkuClassID;
                    customerReturnDetailRptQueryEntity.SKU_ID = this.PreviousPage.SkuID;
                    customerReturnDetailRptQueryEntity.SKU_DESCR = this.PreviousPage.SkuDescr;
                    customerReturnDetailRptQueryEntity.CUSTOMER_ID = this.PreviousPage.CustomerID;
                    customerReturnDetailRptQueryEntity.CUSTOMER_NAME = this.PreviousPage.CustomerName;
                    customerReturnDetailRptQueryEntity.StartDateTime = this.PreviousPage.StartDateTime;
                    customerReturnDetailRptQueryEntity.EndDateTime = this.PreviousPage.EndDateTime;
                }
                else
                {
                    customerReturnDetailRptQueryEntity.OWNER_ID = "";
                    customerReturnDetailRptQueryEntity.SKU_CLASS = "";
                    customerReturnDetailRptQueryEntity.SKU_ID = "";
                    customerReturnDetailRptQueryEntity.SKU_DESCR = "";
                    customerReturnDetailRptQueryEntity.CUSTOMER_ID = "";
                    customerReturnDetailRptQueryEntity.CUSTOMER_NAME = "";
                    customerReturnDetailRptQueryEntity.StartDateTime = "";
                    customerReturnDetailRptQueryEntity.EndDateTime = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            customerReturnDetailRptQueryEntity.IsGetAll = false;
            customerReturnDetailRptQueryEntity.CurrentPage = "0";
            customerReturnDetailRptQueryEntity.PageSize = this.gvReturn.PageSize.ToString();
            customerReturnDetailRptQueryEntity.SortField = "RECEIPT_ID DESC";
            customerReturnDetailRptQueryEntity.SortDirection = "";
            this.PagerFooter.PageSize = this.gvReturn.PageSize;
            this.SetCustomerReturnDetailRptQueryEntity(customerReturnDetailRptQueryEntity);
            this.BindData2gvReturnList(customerReturnDetailRptQueryEntity);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                base.InitAnalyticsReportGridview(this.gvReturn);
                this.LoadData();
            }
        }

        private void SetCustomerReturnDetailRptQueryEntity(CustomerReturnDetailRptQueryEntity customerReturnDetailRptQueryEntity)
        {
            base.PageQueryEntitySession = customerReturnDetailRptQueryEntity;
        }

        public CustomerReturnDetailRptQuery PreviousPage
        {
            get
            {
                return (CustomerReturnDetailRptQuery) base.PreviousPage;
            }
        }
    }
}

